package com.mycase.im.boot.websocket.pattern.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycase.im.boot.entity.communication.ClientMessage;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import com.mycase.im.boot.entity.database.ImChatGroupUser;
import com.mycase.im.boot.entity.database.ImMessage;
import com.mycase.im.boot.service.ChatGroupService;
import com.mycase.im.boot.service.GroupMessageBodyService;
import com.mycase.im.boot.service.MessageService;
import com.mycase.im.boot.service.UserService;
import com.mycase.im.boot.websocket.WebSocketSignal;
import com.mycase.im.boot.websocket.pattern.MessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.List;
import java.util.Map;

/**
 * 用来处理群聊场景的策略器
 * @Author ZhuHJay
 * @Date 2021/9/18 20:55
 */
@Slf4j
@Service
public class GroupChat implements MessageStrategy {

    @Autowired
    private ChatGroupService chatGroupService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private GroupMessageBodyService groupMessageBodyService;
    @Autowired
    private UserService userService;

    @Override
    public Boolean isUse(int type) {
        return WebSocketSignal.MESSAGE_GROUP == type;
    }

    @Override
    public void executeHandler(ClientMessage clientMessage,
                               ObjectMapper objectMapper,
                               Map<String, Session> clientMaps,
                               Session mySession) throws Exception {
        log.warn("onMessage -匹配到-> GroupChat(群组聊天)");

        // 浅拷贝
        ImMessage imMessage = new ImMessage();
        BeanUtils.copyProperties(clientMessage, imMessage);
        // 判断一下是否存在群聊
        if( !chatGroupService.isExist(imMessage.getGroupId()) ){
            sendMessage(objectMapper, mySession, ResponseMessage.error("不存在联系人"));
            return;
        }
        // 创建一条群消息并持久化, 并将传过去的数据修改为一个较为完整的记录
        List<ImChatGroupUser> groupUserList = messageService.createdGroupMessage(imMessage);
        // 补全信息, 数据表有默认值, 但是不会自动回显到JavaBean中: 不是系统通知, 未读消息
        imMessage.setNotify(0);
        imMessage.setReadStatus(0);
        // 获取发送人的个人信息
        imMessage.setFromImUser(userService.getUserInfoById(imMessage.getFromId()));
        // 消息发送(按需发送, 因为每个人获取的聊天记录应该是不同的, 所以要根据在线用户来进行数据获取)
        for (ImChatGroupUser imChatGroupUser : groupUserList) {
            // 如果用户在线则发送消息
            if( clientMaps.containsKey(imChatGroupUser.getUserId()) ){
                // 获取消息体
                imMessage.setImGroupMessageBody(
                        groupMessageBodyService.getOne(imChatGroupUser.getUserId(), imMessage.getToId())
                );
                // 发送数据给客户端
                sendMessage(objectMapper, clientMaps.get(imChatGroupUser.getUserId()),
                            ResponseMessage.success()
                                    .appendData("type", WebSocketSignal.MESSAGE_GROUP)
                                    .appendData("imMessage", imMessage));
            }
        }
    }

}
